import Mock from "mockjs";

import homeLevelData from "./homeLevelData.json";
import homeRegionData from "./homeRegionData.json";
import homeCardData from "./homeCardData.json";
import userTokenData from "./userTokenData.json";
import userCodeData from "./userCodeData.json";

// 定义一个mock接口，返回用户列表
export default [
  {
    url: "/api/home/hospital/level/list",
    method: "get",
    response: () => {
      return {
        code: 200,
        data: homeLevelData.data,
        message: "Okay",
      };
    },
  },
  {
    url: "/api/home/hospital/region/list",
    method: "get",
    response: () => {
      return {
        code: 200,
        data: homeRegionData.data,
        message: "Okay",
      };
    },
  },
  {
    url: "/api/home/hospital/card/list",
    method: "get",
    response: () => {
      return {
        code: 200,
        data: homeCardData.data,
        message: "Okay",
      };
    },
  },
  {
    url: "/api/home/hospital/login",
    method: "post",
    response: (request, response) => {
      let { body } = request;
      let filteredUsers = userTokenData.data;
      if (body.phone) {
        filteredUsers = filteredUsers.filter(user => user.name.includes(body.phone));
      }
      return {
        code: 200,
        data: filteredUsers[0],
        message: "Okay",
        ok: true,
      };
    },
  },
  {
    url: /\/api\/home\/hospital\/code\/(\d+)/,
    method: "get",
    response: (config) => {
      const match = config.url.match(/\/api\/home\/hospital\/code\/(\d+)/);
      let filteredUsers = userCodeData.data;
      if (match && match[1]) {
        const phone = match[1];
        if (phone) {
          filteredUsers = filteredUsers.filter(user => user.name.includes(phone));
        }
      }
      return {
        code: 200,
        data: filteredUsers[0].token,
        message: "Okay",
        ok: true,
      };
    },
  },
];
